<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Common\ajaxReturnJsonContent;
use App\Http\Requests\RoleAddFormCheck;
use App\Role;
use App\Permission;
use DB;


class RolesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $roles = Role::with('perms')->get();
        $permissions = Permission::get(); 

        return View('admin.roles', ['roles' => $roles, 'permissions' => $permissions]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(RoleAddFormCheck $request)
    {
            $role = Role::create([
                'name' => $request->name,
                'display_name' => $request->display_name,
                'description' => $request->description,
            ]);

            //赋予角色权限
            if ($request->perm) {
                $role->attachPermissions($request->perm);
            }

        return back()->with('添加成功');

    }

    /**
     * 用于ajax获取权限名
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request, $name)
    {

       $data = Role::where($request->field, $name)->first();
       return ajaxReturnJsonContent::returnVal($data, '用户名已存在', '');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($name)
    {
        $data = Role::with('perms')->where('name', $name)->first();
        return $data;
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //根据id查询数据
        $role = Role::find($id);
        //当没有查到这个角色或者为admin角色时，重定向回去
        if (!$role) {
            return redirect('admin/Roles')->with(['waring' => '用户名不存在']);;
        }
        if ($role->name == 'admin') {
            return redirect('admin/Roles')->with(['waring' => '没有权限修改']);;
        }
        //更新数
        $role->fill([
            'name' => $request->name,
            'display_name' => $request->display_name,
            'description' => $request->description,
        ])->save();
        //更新权限
        $role->savePermissions($request->perm);
        return back()->with(['waring', '修改成功']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //根据id查询数据
        $role = Role::find($id);
        //当没有查到这个角色或者为admin角色时，重定向回去
        if (!$role or $role->name == 'admin') {
            return redirect('admin/Roles');
        }
        //删除
        $role->delete();
        return back()->with(['waring', '删除成功']);
    }
}
